package com.songtech.server.frame.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

public class ConfUtils {

	private static final Properties prop = new Properties();

	static {
		try {
			prop.load(ConfUtils.class.getResourceAsStream("/config.properties"));
        } catch (Exception e) {
        	try {
				prop.load(new FileInputStream(new File("config.properties")));
			} catch (FileNotFoundException e1) {
				e1.printStackTrace();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
        }
	}
	
	public static String get(String key) {
		return prop.getProperty(key);
	}
	
	@SuppressWarnings("unchecked")
	public static <T> T get(String key, Class<T> type) {
		String value = prop.getProperty(key);
		if(type.isAssignableFrom(Integer.class)) {
			return (T) Integer.valueOf(value);
		} else if(type.isAssignableFrom(Long.class)) {
			return (T) Long.valueOf(value);
		} else if(type.isAssignableFrom(Float.class)) {
			return (T) Float.valueOf(value);
		} else if(type.isAssignableFrom(Double.class)) {
			return (T) Double.valueOf(value);
		} else if(type.isAssignableFrom(Class.class)) {
			try {
				return (T) Class.forName(value);
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
				return null;
			}
		} else {
			return (T) value;
		}
	}
}
